package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    // 订单表
    @Autowired
    private OrderMapper orderMapper;

    // 订单明细表
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    // 地址簿
    @Autowired
    private AddressBookMapper addressBookMapper;
    // 购物车
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     * */
    @Override
    public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
        // 生产地址簿
        AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
        // 处理各类异常(地址簿为空)
        if(addressBook == null){
            throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }
        // 生成用户ID
        Long userId = BaseContext.getCurrentId();
        // 生成购物车实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        // 设置实体类中的用户id
        shoppingCart.setUserId(userId);
        // 操做逻辑层并返回结果
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.lists(shoppingCart);
        // 处理各类异常(购物车为空)
        if(shoppingCartList == null || shoppingCartList.size() == 0){
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        // 生成订单
        Orders orders = new Orders();
        // 数据拷贝
        BeanUtils.copyProperties(ordersSubmitDTO,orders);
        // 设置订单参数
        // 下单时间
        orders.setOrderTime(LocalDateTime.now());
        // 支付状态
        orders.setPayStatus(Orders.UN_PAID);
        // 订单状态
        orders.setStatus(Orders.PENDING_PAYMENT);
        // 订单号
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        // 联系电话
        orders.setPhone(addressBook.getPhone());
        // 收货人
        orders.setConsignee(addressBook.getConsignee());
        // 下单用户ID
        orders.setUserId(userId);
        // 向订单表插入1条数据
        orderMapper.insert(orders);
        List<OrderDetail> orderDetails =new ArrayList<>();

        // 向订单明细表插入n条数据
        for (ShoppingCart cart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
        //     数据拷贝
            BeanUtils.copyProperties(cart,orderDetail);
        //     设置当前订单关联的订单编号
            orderDetail.setOrderId(orders.getId());
        // 添加orderDetail到集合中
            orderDetails.add(orderDetail);
        }
        orderDetailMapper.insertBatch(orderDetails);
        // 清空购物车数据
        shoppingCartMapper.clean(userId);
        // 封装返回结果
        OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
                .id(orders.getId())
                .orderTime(orders.getOrderTime())
                .orderAmount(orders.getAmount())
                .orderNumber(orders.getNumber())
                .build();

        return orderSubmitVO;
    }
}
